use async_trait::async_trait;
use clap::Parser;

use crate::commands::commands::Command;
use crate::models::models::command::CommandState;
use crate::utils::command;

#[derive(Debug, Default, Clone, Parser)]
#[clap(disable_help_flag = true)]
struct Args {
    // 解码
    #[arg(short = 'l', long = "length", default_value_t = 21)]
    length: usize,
}

#[derive(Debug, Default, Clone)]
pub(crate) struct NanoidCommand {}

#[async_trait]
impl Command for NanoidCommand {
    async fn execute(&self, command: crate::models::models::command::Command) -> crate::models::models::command::Command {
        let args = command::parse_command_args::<Args>(command.args.as_str());
        if args.is_err() {
            return self.save_command_output(command, CommandState::Success, args.unwrap_err().to_string());
        }

        let args = args.unwrap();

        let value = nanoid::format(nanoid::rngs::default, &nanoid::alphabet::SAFE, args.length);

        self.save_command_output(command, CommandState::Success, value)
    }
}